package com.DHH.Controller.Api;

import com.DHH.Config.Constant;
import com.DHH.Dto.Message;
import com.DHH.Entity.Base.BaseUser;

import com.DHH.Entity.Order.OrderInfo;
import com.DHH.Entity.Order.OrderInfoState;
import com.DHH.Entity.Order.OrderInfoStateRecord;
import com.DHH.Mapper.Api.Order.ApiOrderInfoMapper;
import com.DHH.Mapper.Order.OrderInfoStateMapper;
import com.DHH.Mapper.Order.OrderInfoStateRecordMapper;
import com.DHH.Service.Api.ApiOrderServices;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by 汪玉东 on 2017/8/6.
 */
@Controller
@RequestMapping("/api/order/")
public class OrderApiController {
    @Resource
    private OrderInfoStateMapper orderInfoStateMapper;
    @Resource
    private ApiOrderInfoMapper apiOrderInfoMapper;

    @Resource
    private OrderInfoStateRecordMapper orderInfoStateRecordMapper;
    @Resource
    private ApiOrderServices apiOrderServices;
    @RequestMapping("create")
    @ResponseBody
    public Message create(String did,String longitude,String latitude,HttpSession session){
        BaseUser baseUser=(BaseUser)session.getAttribute(Constant.USER_IDENTIFICATION);
        Message message=new Message();
        if(baseUser==null){
            message.setState(false);
            message.setMsg("登录失效");
            message.setCode(4001);
            return  message;
        }
        return  apiOrderServices.insert(did,longitude,latitude,baseUser);
    }

    @RequestMapping("list")
    @ResponseBody
    public PageInfo<OrderInfo> getList(PageInfo<OrderInfo> page,HttpSession session){
        BaseUser baseUser=(BaseUser)session.getAttribute(Constant.USER_IDENTIFICATION);

        Example example=new Example(OrderInfo.class);
        example.createCriteria().andEqualTo("userId",baseUser.getId());
        example.orderBy("startTime").desc();
        PageHelper.startPage(page.getPageNum(),10);
        Page<OrderInfo> page1=(Page<OrderInfo>)apiOrderInfoMapper.selectByExample(example);
        page.setList(page1.getResult());
        page.setPageNum(page1.getPageNum());
        page.setPageSize(page1.getPageSize());
        page.setTotal(page1.getTotal());
        page.setPages(page1.getPages());
        return  page;
    }


    @RequestMapping("detail")
    @ResponseBody
    public Message create(int orderId,HttpSession session){
        Message message=new Message();
        BaseUser baseUser=(BaseUser)session.getAttribute(Constant.USER_IDENTIFICATION);

        if(baseUser==null){
            message.setState(false);
            message.setMsg("登录失效");
            message.setCode(4001);
            return  message;
        }
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setId(orderId);
        orderInfo.setUserId(baseUser.getId());
        orderInfo=apiOrderInfoMapper.selectOrderInfoById(orderInfo);
        if(orderInfo.getBaseUser()!=null){
            message.setState(true);
            message.setData(orderInfo);
        }else{
            message.setState(false);
            message.setMsg("获取订单详情失败");
        }
        return  message;
    }

    @RequestMapping("commplete")
    @ResponseBody
    public Message commplete(int orderId,HttpSession session){
        BaseUser baseUser=(BaseUser)session.getAttribute(Constant.USER_IDENTIFICATION);
        Message message=new Message();
        if(baseUser==null){
            message.setState(false);
            message.setMsg("登录失效");
            message.setCode(4001);
            return  message;
        }
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setId(orderId);
        orderInfo.setUserId(baseUser.getId());
        orderInfo=apiOrderInfoMapper.selectOrderInfoById(orderInfo);
       return  apiOrderServices.updateOrderComplete(orderInfo,baseUser);
    }




//    @RequestMapping("orderDolstc")
//    @ResponseBody
//    public Message orderDolstc(int orderId,String longitude,String latitude,HttpSession session){
//        Message message=new Message();
//        OrderInfoState orderInfoState=new OrderInfoState();
//        orderInfoState.setMark("lstc");
//        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);
//        orderInfoState.getId();
//        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();
//        orderInfoStateRecord.setMsg("临时停车");
//        orderInfoStateRecord.setStateId(orderInfoState.getId());
//        orderInfoStateRecord.setOrderId(orderId);
//        orderInfoStateRecord.setCreateTime(new Date());
//        orderInfoStateRecord.setLng(longitude);
//        orderInfoStateRecord.setLat(latitude);
//        orderInfoStateRecordMapper.insert(orderInfoStateRecord);
//        if(orderInfoStateRecord.getId()!=null){
//            message.setState(true);
//            message.setMsg("订单状态修改成功");
//        }else{
//            message.setState(false);
//            message.setMsg("订单状态修改失败");
//        }
//
//        return  message;
//    }
//
//
//    @RequestMapping("orderDotc")
//    @ResponseBody
//    public Message orderDotc(int orderId,String longitude,String latitude,HttpSession session){
//        Message message=new Message();
//        OrderInfoState orderInfoState=new OrderInfoState();
//        orderInfoState.setMark("tc");
//        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);
//        orderInfoState.getId();
//        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();
//        orderInfoStateRecord.setMsg("停车");
//        orderInfoStateRecord.setStateId(orderInfoState.getId());
//        orderInfoStateRecord.setOrderId(orderId);
//        orderInfoStateRecord.setCreateTime(new Date());
//        orderInfoStateRecord.setLng(longitude);
//        orderInfoStateRecord.setLat(latitude);
//        orderInfoStateRecordMapper.insert(orderInfoStateRecord);
//        if(orderInfoStateRecord.getId()!=null){
//            message.setState(true);
//            message.setMsg("订单状态修改成功");
//        }else{
//            message.setState(false);
//            message.setMsg("订单状态修改失败");
//        }
//
//        return  message;
//    }
//
//    @RequestMapping("orderDoFinsh")
//    @ResponseBody
//    public Message orderDoFinsh(int orderId,String longitude,String latitude,HttpSession session){
//        Message message=new Message();
//        OrderInfoState orderInfoState=new OrderInfoState();
//        orderInfoState.setMark("ddwc");
//        orderInfoState=orderInfoStateMapper.selectOne(orderInfoState);
//        orderInfoState.getId();
//        OrderInfoStateRecord orderInfoStateRecord=new OrderInfoStateRecord();
//        orderInfoStateRecord.setMsg("订单完成");
//        orderInfoStateRecord.setStateId(orderInfoState.getId());
//        orderInfoStateRecord.setOrderId(orderId);
//        orderInfoStateRecord.setCreateTime(new Date());
//        orderInfoStateRecord.setLng(longitude);
//        orderInfoStateRecord.setLat(latitude);
//        orderInfoStateRecordMapper.insert(orderInfoStateRecord);
//        if(orderInfoStateRecord.getId()!=null){
//            message.setState(true);
//            message.setMsg("订单状态修改成功");
//        }else{
//            message.setState(false);
//            message.setMsg("订单状态修改失败");
//        }
//
//        return  message;
//    }




}
